export const transfromData = (arr, value)=>{
    let data = arr.filter(item=>item.value==value)
    if(data.length>0){
        return data[0].label
    }
    return ''
}

export const transFormValue = (obj) => {
    let arr = [];
    Object.keys(obj).forEach(function(key){
      if(typeof obj[key] == 'object' ){
        if(obj[key] == null){
          arr.push(`${key}=`)
        }else{
          if(obj[key]=='object'){
            obj[key] = obj[key].map(item=>JSON.stringify(item)).join(',')
          }
          arr.push(`${key}=${JSON.stringify(obj[key])}`)
        }
      }else{
        arr.push(`${key}=${obj[key]}`)
      }
    });
    return '?'+arr.join('&')
}


export const transformData = (data) => { 
  let ret = "";
  for (let it in data) {
    ret += encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
  }
  return ret;
}


// 计算角度
const deg2rad = (deg)=>{
  return deg * (Math.PI/180);
}

//计算两个经纬度之间的距离
export const getDistance = (lat1, lon1, lat2, lon2) =>{
  var R = 6371; // 地球半径，单位为公里
  var dLat = deg2rad(lat2-lat1);
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var distance = R * c; // 距离，单位为公里
  return distance.toFixed(2);
}

export const generateUUID = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

